﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace KzwgRoomTester
{
    public partial class Login : Form
    {
        public Login()
        {
            InitializeComponent();
            initServerList();
        }


        private AccountData account = null;
        private MainWindow mainWindow = null;



        private void btnLogin_Click(object sender, EventArgs e)
        {
            var addr = cbServerHostList.SelectedValue.ToString();
            var name = txtName.Text;
            var password = txtPassword.Text;
            if (string.IsNullOrWhiteSpace(addr))
            {
                Logger.ShowTip("address can not be empty");
                return;
            }

            if (string.IsNullOrWhiteSpace(name))
            {
                Logger.ShowTip("account can not be empty");
                return;
            }

            if (string.IsNullOrWhiteSpace(password))
            {
                Logger.ShowTip("password can not be empty");
                return;
            }


            var loginApi = APIAddr.FullApiUrl(addr, APIAddr.API_AUTH);

            var reply = HttpUtils.Post<AccountData>(loginApi, new { name = name, password = password.GetMD5() });
            if (reply != null && reply.code == 0)
            {
                account = reply.data;
                mainWindow = new MainWindow(account);
                this.Hide();
                mainWindow.ShowDialog();

            }
            else
            {
                Logger.LogError("login failed，reply code {0}", reply.code);
            }
        }


        private void initServerList()
        {

            //string hosts = ConfigurationManager.AppSettings["hosts"];
            var hosts = new List<HostItem>();

            hosts.AddRange(new HostItem[] {
                    new HostItem("http://127.0.0.1:8080", "http://127.0.0.1:8080"),
                    new HostItem("http://119.23.52.209:8081", "http://119.23.52.209:8081"),
                    new HostItem("http://119.23.52.209:8089", "http://119.23.52.209:8089"),
                    new HostItem("http://192.168.3.253:8083", "http://192.168.3.253:8083"),
                    new HostItem("http://192.168.3.253:8084", "http://192.168.3.253:8084"),
                    new HostItem("http://192.168.3.253:8085", "http://192.168.3.253:8085"),
                    new HostItem("http://152.32.164.132:8081", "http://152.32.164.132:8081")
            });
            cbServerHostList.DisplayMember = "Name";
            cbServerHostList.ValueMember = "Host";
            cbServerHostList.DataSource = hosts;
        }

    }


    public class HostItem
    {
        public string Name { get; set; }

        public string Host { get; set; }


        public HostItem(string name, string host)
        {
            Name = name;
            Host = host;
        }

    }
}
